/**
 * Created by sakuri on 2017/6/2.
 * 通知
 */
;-function (window) {
    "use strict";
    var ready = {
        getPath: function(){
            var js = document.scripts, script = js[js.length - 1], jsPath = script.src;
            if(script.getAttribute('merge')) return;
            return jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
        }(),
        loadStyles : function(){
            var csslist = [
                "../gritter/css/jquery.gritter.css"
            ];
            var jslist = [
                "../gritter/js/jquery.gritter.js"
            ];
            var head = $('head')[0];
            var path = ready.getPath;
            $(csslist).each(function(){
                var link = document.createElement('link');
                link.rel = 'stylesheet';
                link.href = path + this;
                head.appendChild(link);
            });
            $(jslist).each(function(){
                $.ajax({
                    url: path + this,
                    dataType: "script",
                    async: false
                });
            });
        },
        uve_server : 'ws://localhost:8080/cmp/information',
        uve_socket : null
    };
    ready.loadStyles();

    var option = {};

    var init = function () {
        core.link_socket();
        init_ready();//加载预设变量
    };

    function init_ready() {//初始化预设值
    }

    //内部核心属性
    var core = {
        link_socket : function(){
            var uve_socket = ready.uve_socket = new WebSocket(ready.uve_server);
            uve_socket.onerror = function(event) {
            };
            uve_socket.onopen = function(event) {
                uve_socket.send('as');
            };
            uve_socket.onmessage = function(event) {
                core.uve_message(event)
            };
            uve_socket.onbeforeunload = function(){
                webSocket.close();
            };

        },
        uve_message : function(event){
            var data = JSON.parse(event.data);
            if(data && data.count > 0){
                var text = [];
                $(data.list).each(function(index,elm){
                    text.push(elm.dateCreated + '<br/>' + elm.content)
                });
                $.gritter.add({
                    title: '异常警告！有 <b>'+data.count+'</b> 条未读',
                    time: '5000',
                    class_name : 'uvedomleniye-window',
                    text: text.join('<br/>')
                });
            }
        }
    };

    //对外公开的方法
    var notice = {
    };
    init();
    //window.notice = notice;
}(window);
